Imports System.Data

Partial Class Admin_Add_Riders
    Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.SetPageAccess(Session("UserName"))
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
        sec = Nothing
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If broker_cmb.Items.Count < 1 Then
            ViewState.Clear()

            Dim zClass As New LookUp.General_Lookup
            broker_cmb.Items.Clear()
            zClass.Loader(broker_cmb, "BROKER", "broker_desc", "broker_cd")
            broker_cmb.Items.Insert(0, "")

            zClass.LookUpCode = "RIDERS"
            zClass.DataGridFill(DataGrid, Results_lbl)

        End If

        Dim Edit_Image As Image = CType(Me.Master.FindControl("L19"), Image)
        Edit_Image.ImageUrl = "~/images/redT.PNG"
        Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("Riders_btn"), LinkButton)
        Edit_Button.ForeColor = Drawing.Color.Black
        Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Lookup_Panel"), Panel)
        Edit_Panel.Visible = True
        SecurePage()
    End Sub

    Protected Sub broker_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles broker_cmb.SelectedIndexChanged
        If broker_cmb.SelectedItem.Text = "" Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        provider_cmb.Items.Clear()
        zClass.Loader(provider_cmb, "PROVIDER", "provider_desc", "provider_cd", broker_cmb.SelectedValue)
        provider_cmb.Items.Insert(0, "")

    End Sub

    Protected Sub provider_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles provider_cmb.SelectedIndexChanged
        If provider_cmb.SelectedItem.Text = "" Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        product_cmb.Items.Clear()
        zClass.Loader(product_cmb, "PRODUCT", "prdt_desc", "prdt_cd", provider_cmb.SelectedValue)
        product_cmb.Items.Insert(0, "")

    End Sub

    Protected Sub product_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles product_cmb.SelectedIndexChanged
        If product_cmb.SelectedItem.Text = "" Or AddEdit_Panel.Visible = True Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "RIDERS"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)

    End Sub

    Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
        OnMode("Add") : ViewState("mode") = "Add"
        Code_txt.Text = "" : Name_txt.Text = ""
        Code_txt.Focus()
    End Sub

    Private Sub OnMode(ByVal mode As String)

        If mode = "Edit" Then
            broker_cmb.Enabled = False
            provider_cmb.Enabled = False
            product_cmb.Enabled = False
            Code_txt.Enabled = False

            Dim dm As New DAOMain.CommonDAO

            Dim dt As DataTable = dm.ExecQuery("SELECT A.broker_cd, B.provider_cd, C.prdt_cd, D.prem_rider_cd" & _
                            " FROM dbo.broker_mstr AS A INNER JOIN" & _
                            " provider_mstr AS B ON A.broker_cd = B.broker_cd INNER JOIN" & _
                            " prdt_mstr AS C ON B.provider_cd = C.provider_cd INNER JOIN" & _
                            " prem_rider_mstr AS D ON C.prdt_cd = D.prdt_cd" & _
                            " Where D.prem_rider_cd = " & dm.Q(Code_txt.Text))

            If dt.Rows.Count > 0 Then
                broker_cmb.SelectedValue = dt.Rows(0)("broker_cd")

                Dim zClass As New LookUp.General_Lookup
                provider_cmb.Items.Clear()
                zClass.Loader(provider_cmb, "PROVIDER", "provider_desc", "provider_cd", broker_cmb.SelectedValue)
                provider_cmb.Items.Insert(0, "")

                provider_cmb.SelectedValue = dt.Rows(0)("provider_cd")
                product_cmb.Items.Clear()
                zClass.Loader(product_cmb, "PRODUCT", "prdt_desc", "prdt_cd", provider_cmb.SelectedValue)
                product_cmb.Items.Insert(0, "")

                product_cmb.SelectedValue = dt.Rows(0)("prdt_cd")
            End If
        End If
        DataGrid.Enabled = False
        Add_btn.Enabled = False
        AddEdit_Panel.Visible = True

    End Sub

    Private Sub NotOnMode()
        broker_cmb.Enabled = True
        provider_cmb.Enabled = True
        product_cmb.Enabled = True
        Code_txt.Enabled = True
        DataGrid.Enabled = True
        Add_btn.Enabled = True
        AddEdit_Panel.Visible = False

    End Sub

    Protected Sub DataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.DeleteCommand
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO

        zClass.ExecQuery("DELETE FROM prem_rider_mstr WHERE prem_rider_cd=" & dm.Q(Trim(e.Item.Cells(0).Text)) & _
          " AND prdt_cd=" & dm.Q(Trim(e.Item.Cells(4).Text)))

        zClass.LookUpCode = "RIDERS"
        If product_cmb.SelectedValue <> "" Then
            zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
        Else
            zClass.DataGridFill(DataGrid, Results_lbl)
        End If

    End Sub

    Protected Sub DataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.EditCommand
        ViewState("mode") = "Edit"
        Code_txt.Text = e.Item.Cells(0).Text
        Name_txt.Text = e.Item.Cells(1).Text
        ViewState("prdt_cd") = Trim(e.Item.Cells(4).Text)
        OnMode("Edit")
        Name_txt.Focus()
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        NotOnMode()
        ViewState.Remove("mode")
        ViewState.Remove("prdt_cd")
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO

        If ViewState("mode") = "Add" Then
            zClass.LookUpCode = "RIDER"
            If zClass.DuplicateCode(Code_txt.Text) = False Then
                Dim pf As New PolyFunc.ASPFunc
                pf.MessageAlert(Me, "Rider Code already exists!")
                pf = Nothing
                Exit Sub
            End If

            zClass.ExecQuery("INSERT INTO prem_rider_mstr(prem_rider_cd, prdt_cd, prem_rider_desc, " & _
                "prem_rider_crea_by, prem_rider_crea_dt) VALUES(" & _
                UCase(dm.Q(Trim(Code_txt.Text))) & ",'" & Trim(product_cmb.SelectedValue.ToString) & "'," & _
                UCase(dm.Q(Trim(Name_txt.Text))) & "," & _
                "'" & UCase(Session("UserName")) & "','" & Now & "')")

        ElseIf ViewState("mode") = "Edit" Then
            zClass.ExecQuery("UPDATE prem_rider_mstr SET " & _
                "prem_rider_desc=" & UCase(dm.Q(Trim(Name_txt.Text))) & ", " & _
                "prem_rider_last_upd_by='" & UCase(Session("UserName")) & "', " & _
                "prem_rider_last_upd_dt='" & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & "' " & _
                "WHERE prem_rider_cd=" & dm.Q(Trim(Code_txt.Text)) & " AND prdt_cd='" & _
                ViewState("prdt_cd") & "'")

        End If

        zClass.LookUpCode = "RIDERS"
        zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
        NotOnMode()
        ViewState.Remove("mode")
        ViewState.Remove("prdt_cd")
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        If product_cmb.SelectedItem.Text = "" Or AddEdit_Panel.Visible = True Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "RIDERS"
        DataGrid.CurrentPageIndex = e.NewPageIndex
        zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
    End Sub
End Class